<template>
  <el-container>
      <!-- 头部区域 -->
    <el-header
      >
      <div class="top_l">
          <img src="../assets/logo.png" alt="">
          <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header
    >
     <!-- 页面主体区 -->
    <el-container>
         <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px':'200px'">
          <div class="toggle_bottom" @click="toggle">折叠</div>
          <el-menu
      :default-active="activePath"
      router
      :collapse="iscollapse"
      :collapse-transition='false'
      unique-opened
      background-color="#333744"
      text-color="#fff"
      active-text-color="#ffd04b">
      <!-- 一级菜单 -->
      <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
        <template slot="title">
          <i class="iconfont" :class="iconObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
          <el-menu-item :index=" '/'+ subItem.path" v-for="subItem in item.children" :key="subItem.id"
            @click="seveNavState('/'+ subItem.path)">
               <template slot="title">
          <i class="el-icon-menu"></i>
          <span>{{subItem.authName}}</span>
        </template>
          </el-menu-item>
      </el-submenu>
    </el-menu>
      </el-aside>
            <!-- 右侧内容区 -->
      <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
        //左侧菜单数据
        menulist:[],
        iconObj:{
            '125':'el-icon-s-custom',
            '103':'el-icon-s-cooperation',
            '101':'el-icon-s-shop',
            '102':'el-icon-s-order',
            '145':'el-icon-s-platform',
        },
        iscollapse:false,
        //被激活的链接地址
        activePath:'',
    };
  },
  created(){
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear("token");
      this.$router.push("/login");
    },
    //获取所有菜单
    async getMenuList(){
      const {data:res} = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg);
       this.menulist = res.data
      console.log(res.data);
    },
    //折叠
    toggle(){
        this.iscollapse = !this.iscollapse
    },
    //保存链接激活状态
    seveNavState(activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath = activePath
    },
  },
};
</script>
<style lang="less" scoped>
.el-container{
    height: 100%;
}
.el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
}
.el-aside{
    background-color: #333744;
    .el-menu{
    border-right: none;
}
}
.el-main{
     background-color: #eaedf1;
}
.top_l {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 22px;
    img{
        width: 50px;
        height: 50px;
        margin-right: 15px;
    }
}
.iconfont{
    margin-right: 10px;
}
.toggle_bottom{
    background-color: #4A5064;
    color: #fff;
    text-align: center;
    line-height: 24px;
    font-size: 10px;
}
</style>
